package game;

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.Serializable;
import java.util.*;
import sprite.Giocatore;
import suono.Sound;

public class GameCore implements Serializable{
	Livello level;
	Integer livelloCorrente;
	public ArrayList<Giocatore> players;
	
	Map<Integer,String> pathLevels;  
	private transient Toolkit t = Toolkit.getDefaultToolkit();
	//se true livelli standard se false livelli editor
	boolean loadType;
	
	//AGGIUNTA VEDI MEGLIO
	public GameCore(GameCore gc) {
		this.livelloCorrente = gc.getLivelloCorrente();
		this.level = gc.getLevel();
		this.players = gc.getPlayers();
		this.pathLevels = gc.getPathLevels();
		this.loadType = true;
	}
	
	public GameCore(int lc,boolean loadType) {
		this.loadType = loadType;
		inizializzaMap();
		livelloCorrente = lc;
		players = new ArrayList<Giocatore>();
	
		//System.out.println(pathLevels.get(livelloCorrente));
		
		Vector<Image> pp = new Vector<Image>();
		pp.add(t.getImage("images/player/mega1.gif"));  //0
		pp.add(t.getImage("images/player/corsa1.gif")); //1
		pp.add(t.getImage("images/player/corsa2.gif")); //2
		pp.add(t.getImage("images/player/corsa3.gif")); //3
		pp.add(t.getImage("images/player/corsa2.gif")); //4	
		
		pp.add(t.getImage("images/player/mega1R.gif"));  //5
		pp.add(t.getImage("images/player/corsa1R.gif")); //6
		pp.add(t.getImage("images/player/corsa2R.gif")); //7
		pp.add(t.getImage("images/player/corsa3R.gif")); //8
		pp.add(t.getImage("images/player/corsa2R.gif")); //9
		
		pp.add(t.getImage("images/player/salto.gif")); //10
		pp.add(t.getImage("images/player/saltoR.gif")); //11
		
		pp.add(t.getImage("images/player/sparo.gif")); //12
		pp.add(t.getImage("images/player/sparo1.gif")); //13
		pp.add(t.getImage("images/player/sparo2.gif")); //14
		pp.add(t.getImage("images/player/sparo3.gif")); //15
		pp.add(t.getImage("images/player/sparo2.gif")); //16
		
		pp.add(t.getImage("images/player/sparoR.gif")); //17
		pp.add(t.getImage("images/player/sparo1R.gif")); //18
		pp.add(t.getImage("images/player/sparo2R.gif")); //19
		pp.add(t.getImage("images/player/sparo3R.gif")); //20
		pp.add(t.getImage("images/player/sparo2R.gif")); //21
		
		pp.add(t.getImage("images/player/morto1.gif")); //22
		pp.add(t.getImage("images/player/morto2.gif")); //23
		pp.add(t.getImage("images/player/morto3.gif")); //24
		pp.add(t.getImage("images/player/morto4.gif")); //25
		pp.add(t.getImage("images/player/morto5.gif")); //26

		pp.add(t.getImage("images/player/morto1R.gif")); //27
		pp.add(t.getImage("images/player/morto2R.gif")); //28
		pp.add(t.getImage("images/player/morto3R.gif")); //29
		pp.add(t.getImage("images/player/morto4R.gif")); //30
		pp.add(t.getImage("images/player/morto5R.gif")); //31
		
		
		Livello level2 = new Livello(pathLevels.get(livelloCorrente),loadType);
		players.add(new Giocatore(4*50,10*50,level2.width*Livello.TILE_SIZE,pp,null));
		
		level = new Livello(pathLevels.get(livelloCorrente),players,livelloCorrente,loadType);
		players.get(0).setMappa(level.getMappa());

		//players.get(0).img.add(t.getImage("images/mega1.gif"));
	}
	
	public void addPlayer(Giocatore g) {
		players.add(g);
	}
	

	private void inizializzaMap() {
		pathLevels = new HashMap<Integer,String>();
		if(loadType) {
			pathLevels.put(1, "level/uno.rgl");
			pathLevels.put(2, "level/due.rgl");
			pathLevels.put(3, "level/tre.rgl");
			pathLevels.put(4, "level/quattro.rgl");
			pathLevels.put(5, "level/cinque.rgl");
		} else {
			File livelliEditor = new File("editor/levels");
			String s[] = livelliEditor.list();
			for (int i=0; i<s.length; i++){
				pathLevels.put(i, "editor/levels/"+s[i]);
			}
		}
		
	}
	

	public Livello getLevel() {
		return level;
	}

	public void setLevel(Livello level) {
		this.level = level;
	}

	public Integer getLivelloCorrente() {
		return livelloCorrente;
	}

	public void setLivelloCorrente(Integer livelloCorrente) {
		this.livelloCorrente = livelloCorrente;
	}

	public ArrayList<Giocatore> getPlayers() {
		return players;
	}

	public void setPlayers(ArrayList<Giocatore> players) {
		this.players = players;
	}

	public Map<Integer, String> getPathLevels() {
		return pathLevels;
	}

	public void setPathLevels(Map<Integer, String> pathLevels) {
		this.pathLevels = pathLevels;
	}

}
